package model.dao.registerOrder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import log.MyLogger;
import model.service.Enterprise;
import model.service.Person;

public class EnterpriseDAOImpl{

	String sql = "" ;
	db.dbconnection dbconnection ;
	Connection myConnection ;
	Statement st ;
	
	public List<Enterprise> getEnterprisesByName(String name) {
		List<Enterprise> enterprises = new ArrayList<Enterprise>();
		
		try {
			
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "SELECT * from enterprise where enterprise.name ILIKE '"+name+"%'";
			ResultSet resultSet = st.executeQuery(sql);
			
			while(resultSet.next()){
				Enterprise e = new Enterprise(resultSet.getInt("enterprise"));
				e.setCreated(resultSet.getTimestamp("created"));
				Person cb = new Person();
				cb.setPerson(resultSet.getInt("created_by"));
				e.setCreatedBy(cb);
				e.setEnterprise(resultSet.getInt("enterprise"));
				e.setFullName(resultSet.getString("full_name"));
				e.setName(resultSet.getString("name"));
				Person ub = new Person();
				ub.setPerson(resultSet.getInt("updated_by"));
				e.setUpdatedBy(ub);
				enterprises.add(e);
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("EnterpriseDAOImpl.getEnterprisesByName()", e.getMessage());
		}
		return enterprises;
	}

	public Enterprise getEnterpriseById(int enterpriseID) {
		Enterprise enterprise = new Enterprise();
		enterprise.setEnterprise(enterpriseID);
		try {
			Connection c = dbconnection.getConnection();
			
			
			Statement st = c.createStatement();
			String sql = new String("Select * from enterprise where enterprise="+enterpriseID);
			ResultSet resultSet = st.executeQuery(sql);
			
			while(resultSet.next()){
				enterprise.setName(resultSet.getString("name"));
				
				enterprise.setFullName(resultSet.getString("full_name"));
				
				enterprise.setCreated(resultSet.getTimestamp("created"));
				
				Person cb = new Person();
				cb.setPerson(resultSet.getInt("created_by"));
				enterprise.setCreatedBy(cb);
				
				Person ub = new Person();
				ub.setPerson(resultSet.getInt("updated_by"));
				enterprise.setUpdatedBy(ub);
				
				enterprise.setUpdated(resultSet.getTimestamp("updated"));
			}
			this.dbconnection.close();
	} catch (Exception e) {
//		MyLogger.Log("EnterpriseDAOImpl.getEnterpriseById()", e.getMessage());
	}
		return enterprise;
	}

}
